package cn.com.twoke.application.game.mario.core.ecs.entity;


import cn.com.twoke.application.game.mario.core.ecs.component.Component;
import lombok.Getter;
import lombok.Setter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;

// 实体类
public class Entity {
    @Getter
    private final int id;
    @Getter
    @Setter
    private  Object type;
    @Getter
    @Setter
    private int parentId;
    private final Map<Class<? extends Component>, Component> components = new HashMap<>();

    public Entity(int id) {
        this.id = id;
    }

    public <T extends Component> void addComponent(T component) {
        components.put(component.getClass(), component);
    }

    public <T extends Component> T getComponent(Class<T> componentClass) {
        return componentClass.cast(components.get(componentClass));
    }

    public boolean hasComponent(Class<? extends Component> componentClass) {
        return components.containsKey(componentClass);
    }

    public void removeComponent(Class<? extends Component> componentClass) {
        components.remove(componentClass);
    }


    public List<Component> getComponents(Predicate<Component> predicate) {
        return components.values().stream().filter(predicate).collect(Collectors.toList());
    }
}